package com.hue.controller;


import com.hue.entity.EduSubject;
import com.hue.entity.R;
import com.hue.entity.excel.TreeData;
import com.hue.service.EduSubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 课程科目 前端控制器
 * </p>
 *
 * @author 姚凤桥
 * @since 2021-06-12
 */
@RestController
@CrossOrigin
@RequestMapping("/subject")
public class EduSubjectController {
    @Autowired
    private EduSubjectService eduSubjectService;

    //读取上传的文件
    @PostMapping("/upload")
    public R upload(MultipartFile file){
        try {
            eduSubjectService.upload(file);
            return R.ok();
        } catch (IOException e) {
            e.printStackTrace();
            return R.error();
        }
    }

    //返回所有分类信息
    @GetMapping("/findAll")
    public R findAll(){
        List<TreeData> subects = eduSubjectService.findAll();
        return R.ok().data("subjects",subects);
    }

    //查询所有以及分类
    @GetMapping("/findAllOneSubject")
    public R findAllOneSubject(){
        List<EduSubject> oneSubjects = eduSubjectService.findAllOneSubject();
        return R.ok().data("oneSubjects",oneSubjects);
    }

    //根据parent_id查询所有二级分类
    @GetMapping("/findAllTwoSubject/{parentId}")
    public R findAllTwoSubject(@PathVariable("parentId") String parentId){
        List<EduSubject> twoSubjects = eduSubjectService.findAllTwoSubject(parentId);
        return R.ok().data("twoSubjects",twoSubjects);
    }
}

